package model;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for Score
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see model.Score
 * @author MyEclipse Persistence Tools
 */

public class ScoreDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(ScoreDAO.class);
	// property constants
	public static final String STUDENTCOURSE_ID = "studentcourseId";
	public static final String PINGSHIFEN = "pingshifen";
	public static final String KAOSHIFEN = "kaoshifen";

	protected void initDao() {
		// do nothing
	}
	
	/**
	 * 根据学生id，获得该学生的成绩信息
	 */
	public List getScoreBystuId(String stuId)
	{
		String hql="FROM Score AS sc," +
		"Studentcourse AS stco," +
		"Student AS st,Course AS co,Teacher AS te," +
		"Special AS sp,Department AS de," +
		"Coursetype AS coty " +
		"WHERE sc.studentcourseId=stco.id" +
		" and stco.stuId=st.id and stco.courseId=co.id and stco.teacherId=te.id" +
		" and st.stuspecialId=sp.id and sp.specialdepartmentId=de.id" +
		" and co.coursetypeId=coty.id";
		String temp="";
		String clause=temp;
		List searchlist=null;
		
			if(stuId!=null&&!"".equals(stuId))
			{
				clause=clause+" and st.id=:stuId";
			}
			
			if(!clause.equals(temp))
			{
				hql=hql+" "+clause;
			}
			System.out.println("--------------------------");
			System.out.println(hql);
			Query q=super.getSession().createQuery(hql);
		
			if(stuId!=null&&!"".equals(stuId))
			{
				q.setInteger("stuId", Integer.parseInt(stuId));
			}
			searchlist=q.list();
		
		return searchlist;
	}

	/**
	 * 根据成绩表id获得记录
	 * @return
	 */
	public List getScorebyId(String id)
	{

		String hql="FROM Score AS sc," +
				"Studentcourse AS stco," +
				"Student AS st,Course AS co,Teacher AS te," +
				"Special AS sp,Department AS de," +
				"Coursetype AS coty " +
				"WHERE sc.studentcourseId=stco.id" +
				" and stco.stuId=st.id and stco.courseId=co.id and stco.teacherId=te.id" +
				" and st.stuspecialId=sp.id and sp.specialdepartmentId=de.id" +
				" and co.coursetypeId=coty.id";
		String temp="";
		String clause=temp;
		List searchlist=null;
		
			if(id!=null&&!"".equals(id))
			{
				clause=clause+" and sc.id=:id";
			}
			
			if(!clause.equals(temp))
			{
				hql=hql+" "+clause;
			}
			System.out.println("--------------------------");
			System.out.println(hql);
			Query q=super.getSession().createQuery(hql);

			if(id!=null&&!"".equals(id))
			{
				q.setInteger("id", Integer.parseInt(id));
			}
			searchlist=q.list();
		
		return searchlist;
	}
	
	//get all score info
	public List getAllScoreInfo(String rightsql)
	{
		String hql="FROM Score AS sc," +
				"Studentcourse AS stco," +
				"Student AS st,Course AS co,Teacher AS te," +
				"Special AS sp,Department AS de," +
				"Coursetype AS coty " +
				"WHERE sc.studentcourseId=stco.id" +
				" and stco.stuId=st.id and stco.courseId=co.id and stco.teacherId=te.id" +
				" and st.stuspecialId=sp.id and sp.specialdepartmentId=de.id" +
				" and co.coursetypeId=coty.id";
		if(!rightsql.equals(""))
			hql=hql+" and "+rightsql;
		Query q=super.getSession().createQuery(hql);
		List list = q.list();
		return list;
	}
	
	//search score
	public List searchScoreInfo(String stunumber,String stuname,String stusex,String stuspecialId,String specialdepartmentId,String coursename,String coursetypeId,String rightsql)
	{
		
		String hql="FROM Score AS sc," +
				"Studentcourse AS stco," +
				"Student AS st,Course AS co,Teacher AS te," +
				"Special AS sp,Department AS de," +
				"Coursetype AS coty " +
				"WHERE sc.studentcourseId=stco.id" +
				" and stco.stuId=st.id and stco.courseId=co.id and stco.teacherId=te.id" +
				" and st.stuspecialId=sp.id and sp.specialdepartmentId=de.id" +
				" and co.coursetypeId=coty.id";
		String temp="";
		String clause=temp;
		List searchlist=null;
		
			if(stunumber!=null&&!"".equals(stunumber))
			{
				clause=clause+" and st.stunumber like :stunumber";
			}
			if(stuname!=null&&!"".equals(stuname))
			{
				clause=clause+" and st.stuname like :stuname";
			}
			if(stusex!=null&&!"".equals(stusex)&&!stusex.equals("0"))
			{
				clause=clause+" and st.stusex=:stusex";
			}
			if(stuspecialId!=null&&!"".equals(stuspecialId)&&!stuspecialId.equals("0"))
			{
				clause=clause+" and st.stuspecialId=:stuspecialId";
			}
			if(specialdepartmentId!=null&&!"".equals(specialdepartmentId)&&!specialdepartmentId.equals("0"))
			{
				clause=clause+" and de.id=:specialdepartmentId";
			}
			if(coursename!=null&&!"".equals(coursename))
			{
				clause=clause+" and co.coursename like :coursename";
			}
			if(coursetypeId!=null&&!"".equals(coursetypeId))
			{
				clause=clause+" and coty.coursetypeId=:coursetypeId";
			}
			
			if(!clause.equals(temp))
			{
				hql=hql+" "+clause;
			}
			if(!rightsql.equals(""))
				hql=hql+" and "+rightsql;
			System.out.println("--------------------------");
			System.out.println(hql);
			Query q=super.getSession().createQuery(hql);
			if(stunumber!=null&&!"".equals(stunumber))
			{
				q.setString("stunumber", '%'+stunumber+'%');
			}
			if(stuname!=null&&!"".equals(stuname))
			{
				q.setString("stuname", '%'+stuname+'%');
			}
			if(stusex!=null&&!"".equals(stusex)&&!stusex.equals("0"))
			{
				q.setString("stusex", stusex);
			}
			if(stuspecialId!=null&&!"".equals(stuspecialId)&&!stuspecialId.equals("0"))
			{
				q.setInteger("stuspecialId", Integer.parseInt(stuspecialId));
			}
			if(specialdepartmentId!=null&&!"".equals(specialdepartmentId)&&!specialdepartmentId.equals("0"))
			{
				q.setInteger("specialdepartmentId", Integer.parseInt(specialdepartmentId));
			}
			if(coursename!=null&&!"".equals(coursename))
			{
				q.setString("coursename", '%'+coursename+'%');
			}
			if(coursetypeId!=null&&!"".equals(coursetypeId))
			{
				q.setString("coursetypeId", coursetypeId);
			}
			searchlist=q.list();
		
		return searchlist;
	}
	
	public void save(Score transientInstance) {
		log.debug("saving Score instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void edit(String id,Float pingshifen,Float kaoshifen)
	{
		String hql="UPDATE Score AS s SET s.pingshifen=:pingshifen,s.kaoshifen=:kaoshifen WHERE s.id=:id";
		Query q=super.getSession().createQuery(hql);
		q.setInteger("id", Integer.parseInt(id));
		q.setFloat("pingshifen", pingshifen);
		q.setFloat("kaoshifen", kaoshifen);
		q.executeUpdate();
	}
	
	public void deleteScoreById(String id)
	{
		String hql="DELETE Score AS s WHERE s.id=:id";
		Query q=super.getSession().createQuery(hql);
		q.setInteger("id", Integer.parseInt(id));
		q.executeUpdate();
	}
	
	public void delete(Score persistentInstance) {
		log.debug("deleting Score instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Score findById(java.lang.Integer id) {
		log.debug("getting Score instance with id: " + id);
		try {
			Score instance = (Score) getHibernateTemplate().get("model.Score",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Score instance) {
		log.debug("finding Score instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Score instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Score as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByStudentcourseId(Object studentcourseId) {
		return findByProperty(STUDENTCOURSE_ID, studentcourseId);
	}

	public List findByPingshifen(Object pingshifen) {
		return findByProperty(PINGSHIFEN, pingshifen);
	}

	public List findByKaoshifen(Object kaoshifen) {
		return findByProperty(KAOSHIFEN, kaoshifen);
	}

	public List findAll() {
		log.debug("finding all Score instances");
		try {
			String queryString = "from Score";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Score merge(Score detachedInstance) {
		log.debug("merging Score instance");
		try {
			Score result = (Score) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Score instance) {
		log.debug("attaching dirty Score instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Score instance) {
		log.debug("attaching clean Score instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ScoreDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ScoreDAO) ctx.getBean("ScoreDAO");
	}
}